<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Util\SqlServerUtil;
use App\Util\Common;
use App\Services\OSS;
use App;

class UserController extends Controller
{
    protected $response = [
		"code" => 200,
		"message" => "接口请求成功",
		"result" => []
	];

	protected $SqlServer = null;

	public function __construct(Request $request){
		$temp = SqlServerUtil::getModel($request);
		if($temp){
			$this->SqlServer = $temp['SqlServer'];
		}
	}

	//获取用户列表
	public function getUserLists(Request $request){
		$page= $request ->get('page');
		if(!isset($page)) $page =1;
		$limit= $request ->get('limit');
		if(!isset($limit)) $limit =20;
		$pages = ($page-1)* $limit;
		
		$search ='';
		if ($request->has('keyword') and $request->keyword != '') {
			$search = " and username like '%".$request->keyword."%'";
			
		}
		$sql ="SELECT TOP {$limit} * FROM yd_user u where u.id NOT IN (SELECT TOP $pages u.id FROM yd_user u where u.status=1 {$search}) and u.status=1 {$search}";
		
		$res = SqlServerUtil::exec($this->SqlServer,$sql);
		
		$countSql ="SELECT COUNT(1) AS total FROM yd_user u where u.status=1 {$search}";
		$count = SqlServerUtil::exec($this->SqlServer, $countSql);
		$this->response['total'] = $count ? $count[0]['total']:0;
		$this->response['size'] = $page;
		$last_page =0;
		if($count && count($count)>0){
			$last_page = ceil($count[0]['total']/$limit);
		}
		
		$this->response['result'] = $res;
		$this->response['last_page'] =$last_page;
		return response()->json($this->response);
	}

	//添加客户
	public function addUser(Request $request){
		
		$username =$request->get("username");
		$password =$request->get("password");
		$customer =$request->get("customer");
		$customer_sn =$request->get("customer_sn");
		$email =$request->get("email");
		$address =$request->get("address");
		$birthday =strtotime($request->get("birthday"));
		
		if(empty($username)){
			return response()->json(Common::errorResponse(452));
		}
		if(empty($customer)){
			return response()->json(Common::errorResponse(453));
		}
		if(empty($customer_sn)){
			return response()->json(Common::errorResponse(454));
		}
		if(empty($birthday)){
			return response()->json(Common::errorResponse(455));
		}
		//用户名是否存在
		$user = SqlServerUtil::exec($this->SqlServer,"select id from yd_user where username={$username} and status=1");
		if(!empty($user)){
			return response()->json(Common::errorResponse(456));
		}
		
		$data = [
			"username" => iconv('UTF-8', 'GBK',$username),
			"password" => iconv('UTF-8', 'GBK',$password),
			"customer" => iconv('UTF-8', 'GBK',$customer),
		    "customer_sn" => iconv('UTF-8', 'GBK',$customer_sn),
			"email" => iconv('UTF-8', 'GBK',$email),
		    "address" => iconv('UTF-8', 'GBK',$address),
			"birthday" => iconv('UTF-8', 'GBK',$birthday),
		    "sex" => $request->get("sex"),
		    "create_time" => time(),
		];
		$this->response['result'] = SqlServerUtil::create($this->SqlServer,"yd_user",$data);
		return response()->json(Common::successResponse(200,'用户添加成功'));
	}

	//修改密码
	public function updatePassword(Request $request)
	{
		$res = SqlServerUtil::update($this->SqlServer,"yd_user",['password' => $request->get('password')],['id' => $request->get('id')]);
		if($res){
			$this->response['message'] = "密码修改成功";
		}else{
			$this->response['code'] = 400;
			$this->response['message'] = "密码修改失败";
		}
		return response()->json($this->response);
	}

	//删除用户账号 ---status置0
	public function delUser(Request $request)
	{
		//wxid_5k1jfzr3q5xe22
		$response['result'] = SqlServerUtil::execSql($this->SqlServer,"update  yd_user set status=0  WHERE id = ".$request->get("id"));
		
		return response()->json($this->response);
	}

	//获取单个用户信息
	public function getUserInfo($id)
	{
		$this->response['result'] = SqlServerUtil::get($this->SqlServer,"yd_user",['id' => $id],['*']);
		return response()->json($this->response);
	}

	//编辑用户
	public function editUser(Request $request)
	{
		//$username =$request->get("username");
		$password =$request->get("password");
		$customer =$request->get("customer");
		$customer_sn =$request->get("customer_sn");
		$email =$request->get("email");
		$address =$request->get("address");
		$birthday =strtotime($request->get("birthday"));
		
		if(empty($customer)){
			return response()->json(Common::errorResponse(453));
		}
		if(empty($customer_sn)){
			return response()->json(Common::errorResponse(454));
		}
		if(empty($birthday)){
			return response()->json(Common::errorResponse(455));
		}
		
		$data = [
			
			"password" => iconv('UTF-8', 'GBK',$password),
			"customer" => iconv('UTF-8', 'GBK',$customer),
			"customer_sn" => iconv('UTF-8', 'GBK',$customer_sn),
			"email" => iconv('UTF-8', 'GBK',$email),
			"address" => iconv('UTF-8', 'GBK',$address),
			"birthday" => iconv('UTF-8', 'GBK',$birthday),
			"sex" => $request->get("sex"),
			"update_time" => time(),
			'status'=> $request->get('status'),
		];
		SqlServerUtil::update($this->SqlServer,"yd_user",$data, ['id' => $request->get('id')]);
		return response()->json(Common::successResponse(200,'用户编辑成功'));
	}

}
